package com.rensea.android.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.rensea.android.objs.Message;

public class ListMessageAdapter extends BaseAdapter {

    private Context mContext;
    private Message[] messages;
    private int count = 0; 
    private boolean moreBtn = true;
    private Button moreButton = null;
    
    public ListMessageAdapter(Context context,Message[] msgs,Button moreButton) {
        mContext = context;
        messages = msgs;
        this.moreButton = moreButton;
        initCount();
    }
    private void initCount(){
    	moreBtn = true;
        count = null==messages?0:messages.length;
        if(count==0){
        	moreBtn = false;
        }else if(count%20!=0){
        	moreBtn = false;
        }
    }
    public void addMessages(Message[] msgs){
    	Message[] tmpMsg = new Message[messages.length+msgs.length];
    	for(int i=0;i<messages.length;i++){
    		tmpMsg[i] = messages[i];
    	}
    	int fCount = messages.length;
    	for(int i=0;i<msgs.length;i++){
    		tmpMsg[fCount+i] = msgs[i];
    	}
    	messages = tmpMsg;
    	initCount();
    	this.notifyDataSetChanged();
    }
	@Override
	public int getCount() {
		return count+(moreBtn?1:0);
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(position==count&&moreBtn){
			return moreButton;
		}else{
			return new MessageView(mContext,messages[position]);
		}
	}

}
